﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_DAL;
using Zor_Modes;
using Zor_Tools;
namespace Zor_Bll
{
  public  class Zor_Evaluate_Bll <T> where T : class
    { 
        #region - 评定管理列表 -

        /// <summary>
        /// 获取评议管理列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageList(dynamic Model,int index, out int TotalCount, string where)
        {
            int Dtid = RequestSession.GetSessionUser().DT_Id; //获取当前登录人的部门ID
            if (Model.Tenant_Name == null &&
                Model.Lease_StartTime == null &&
                Model.NatPer_Name == null &&
                Model.NatPer_Phone == null &&
                Model.NatPer_IDNumber == null &&
                Model.Acceptance_TicketNumber == null &&
                Model.Acceptance_TaxNumber == null &&
                Model.State == null &&
                Model.AcceptanceType == null
                )
            {
                var sqlSurch = "  Dt_Id  in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId=" + Dtid + ")) and Evaluate_State in (0,2)";
                return PagerHelp<T>.GetPagerList(index, "V_Zor_Evaluate", "Review_Date desc", out TotalCount, sqlSurch);
            }

            return PagerHelp<T>.GetPagerList(index, "V_Zor_Evaluate", "Review_Date desc ", out TotalCount, GetSearchWhere(Model, "   Dt_Id  in  ( select Dt_Id  from dbo.Sys_Department where  Dt_ParentId in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId=" + RequestSession.GetSessionUser().DT_Id + ")) and Evaluate_State<>3"));
        }
     
        /// <summary>
        /// 推送提交至审核
        /// create zhl by 17.3.31
        /// </summary>
        /// <param name="evaluate">评定对象</param>
        /// <returns>2--当前评议已推送-1操作失败1--操作成功的标识</returns>
        public static string Evaluate(Zor_Evaluate evaluate, string accpid)
        {
            var messge = "";
            evaluate.Evaluate_Rent = evaluate.Evaluate_Rent * 100;
            int narid = Zor_Evaluate_DAL<T>.Evaluate(evaluate, accpid);
            if (narid == 1)
            {
                messge = "推送并评定成功";
            }
            else if (narid == 2)
            {
                messge = "当前评定已推送评定，不能重复推送";
            }
            else
            {
                messge = "推送评定失败";
            }
            return messge;
        
        }
        /// <summary>
        /// 退回至受理单
        /// create zhl by 17.3.31
        /// 评定状态(0正在评定,1已评定,2被退回,3退回到评议)
        /// </summary>
        /// <param name="aptid">受理单id</param>
        /// <param name="reviewid">评议id</param>
        /// <param name="reviewid">退回意见</param>
        /// <returns></returns>
        public static BaseAnswer ReturnReview(Zor_Evaluate evaluate, string accpid)
        {

            BaseAnswer bar = Zor_Evaluate_DAL<T>.ReturnReview(evaluate, accpid);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "退回成功！" : "退回失败！";
            return bar;        
        }

        #region 私有函数

        private static string GetSearchWhere(dynamic Model, string where)
        {
            var sqlSurch = string.Empty;

            if (Model.Tenant_Name != null)
            {
                sqlSurch += " and Tenant_Name like '%" + Model.Tenant_Name + "%'";
            }
            if (Model.Lease_StartTime != null && Model.Lease_EndTime != null)
            {
                sqlSurch += " and Lease_StartTime >=" + TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_StartTime.ToString())) + " and Lease_EndTime<=" + TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_EndTime.ToString())) + "";
            }
            if (Model.NatPer_Name != null)
            {
                sqlSurch += " and NatPer_Name like '%" + Model.NatPer_Name + "%'";
            }
            if (Model.NatPer_Phone != null)
            {
                sqlSurch += " and NatPer_Phone = '" + Model.istaxregistration + "'";
            }
            if (Model.NatPer_IDNumber != null)
            {
                sqlSurch += " and NatPer_IDNumber = '" + Model.NatPer_IDNumber + "'";
            }
            if (Model.Acceptance_TicketNumber != null)
            {
                sqlSurch += " and Acceptance_TicketNumber = '" + Model.Acceptance_TicketNumber + "'";
            }
             if (Model.State != null)
            {
                if (Model.State == "全部")
                {
                    sqlSurch += " and State in ('已保存','已评定','退回至评议','被退回')";     
                }
                else
                {
                    sqlSurch += " and State = '" + Model.State + "'";
                }              
            }
             if (Model.AcceptanceType != null)
             {
                 if (Model.AcceptanceType == "全部")
                 {
                     sqlSurch += " and AcceptanceType in('社区','自然人')";
                 }
                 else
                 {
                     sqlSurch += " and AcceptanceType = '" + Model.AcceptanceType + "' ";
                 }
             }
            return where + sqlSurch;
        }
        #endregion

        #endregion
    }
}
